package br.senac.sp.uniobjetos;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import br.senac.sp.persistence.AbstractRelationalDBMapper;
import br.senac.sp.persistence.unityofwork.Session;

public class MatriculaMapper extends AbstractRelationalDBMapper<Matricula, Integer>{

    public MatriculaMapper(String tableName) {
        super(tableName);
    }

    @Override
    protected Matricula getObjectFromRecord(Integer oid, ResultSet record)
            throws SQLException {
        Session session = new Session();
        Matricula matricula = new Matricula();
        matricula.setAluno(session.get(record.getInt("ID_ALUNO"), Aluno.class));
        matricula.setTurma(session.get(record.getInt("ID_TURMA"), Turma.class));
        matricula.setDataMatricula(record.getDate("DATA_MATRICULA"));
        return matricula;
    }

    @Override
    protected Map<String, Object> getInfoFromObject(Matricula entity) {
        Map<String, Object> info = new HashMap<String, Object>();
        info.put("ID_ALUNO", entity.getAluno().getIdAluno());
        info.put("ID_TURMA", entity.getTurma().getIdTurma());
        info.put("DATA_MATRICULA", entity.getDataMatricula());
        return info;
    }

    @Override
    protected Integer getIdFromObject(Matricula entity) {
        // TODO O que fazer no caso de uma chave composta??
        return null;
    }

    @Override
    protected String getUniqueIdentifierArguments() {
        return "ID_ALUNO=? AND ID_TURMA=?"; //TODO como fazer isso
    }

}
